<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组织架构" prop="orgId">
        <el-select
          v-model="queryParams.orgId"
          placeholder="请选择组织架构"
          clearable
          filterable
          size="small"
          style="width: 230px"
        >
          <el-option
            v-for="item in deptTreeOptions"
            :key="item.deptId"
            :label="item.label"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="系统名称" prop="systemName">
        <el-input
          v-model="queryParams.systemName"
          placeholder="请输入系统名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="系统级别" prop="systemLevel">
        <el-select
          v-model="queryParams.systemLevel"
          placeholder="请选择系统级别"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.sys_level"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="系统类型" prop="systemType">
        <el-select v-model="queryParams.systemType" placeholder="请选择系统类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_type"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="信息类别" prop="infoType">
        <el-select v-model="queryParams.infoType" placeholder="请选择信息类别" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_info_type"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="备案编号" prop="filingNumber">
        <el-input
          v-model="queryParams.filingNumber"
          placeholder="请输入备案编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="访问途径" prop="accessMethod">
        <el-input
          v-model="queryParams.accessMethod"
          placeholder="请输入访问途径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="开发单位" prop="developCompany">
        <el-input
          v-model="queryParams.developCompany"
          placeholder="请输入系统开发单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运维单位" prop="maintainCompany">
        <el-input
          v-model="queryParams.maintainCompany"
          placeholder="请输入系统运维单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主管单位" prop="businessManager">
        <el-input
          v-model="queryParams.businessManager"
          placeholder="请输入系统业务主管单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:asset:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:asset:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:asset:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:asset:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table 
      v-loading="loading" 
      :data="assetList" 
      @selection-change="handleSelectionChange" 
      @expand-change="handleExpandChange"
      ref="assetTable"
      row-key="assetId"
      :expand-row-keys="expandRowKeys"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <div style="padding: 0 20px;">
            <p v-if="props.row.gxLoading">加载中...</p>
            <p v-if="!props.row.gxList || props.row.gxList.length === 0">暂无数据</p>
            <el-table 
              v-loading="props.row.gxLoading" 
              :data="props.row.gxList" 
              border 
              style="width: 65%;"
              size="small"
              v-if="props.row.gxList && props.row.gxList.length > 0"
            >
              <el-table-column label="ID" prop="id" align="center" width="60" />
              <el-table-column label="访问域名" prop="accessDomain" align="center" min-width="120" />
              <el-table-column label="访问IP地址" prop="accessIp" align="center" min-width="120" />
              <el-table-column label="本地开放端口" prop="localPort" align="center" width="100" />
              <el-table-column label="公网映射端口" prop="publicMappedPort" align="center" width="100" />
              <el-table-column label="服务器IP地址" prop="serverIp" align="center" min-width="120" />
              <el-table-column label="是否有效" prop="isActive" align="center" width="80">
                <template slot-scope="scope">
                  {{ scope.row.isActive === 1 ? '是' : '否' }}
                </template>
              </el-table-column>
              <el-table-column label="创建时间" prop="createTime" align="center" width="150">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="更新时间" prop="updateTime" align="center" width="150">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.updateTime) }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资产ID" align="center" prop="assetId" v-if="columns[0].visible" />
      <el-table-column label="组织架构" align="center" prop="orgIdName" v-if="columns[1].visible" />
      <el-table-column label="单位类型" align="center" prop="orgTypeName" v-if="columns[2].visible" />
      <el-table-column label="系统名称" align="center" prop="systemName" v-if="columns[3].visible" />
      <el-table-column label="系统类型" align="center" prop="systemTypeName" v-if="columns[4].visible" />
      <el-table-column label="信息类别" align="center" prop="infoTypeName" v-if="columns[5].visible" />
      <el-table-column label="是否定级" align="center" prop="isClassifiedName" v-if="columns[6].visible" />
      <el-table-column label="系统级别" align="center" prop="systemLevelName" v-if="columns[7].visible" />
      <el-table-column label="信息安全等级" align="center" prop="businessSecurityLevelName" v-if="columns[8].visible" />
      <el-table-column label="系统安全等级" align="center" prop="serviceSecurityLevelName" v-if="columns[9].visible" />
      <el-table-column label="是否备案" align="center" prop="isFiledName" v-if="columns[10].visible" />
      <el-table-column label="备案编号" align="center" prop="filingNumber" v-if="columns[11].visible" />
      <el-table-column label="是否已测评" align="center" prop="isEvaluatedName" v-if="columns[12].visible" />
      <el-table-column label="最近测评时间" align="center" prop="lastEvaluateTime" width="180" v-if="columns[13].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastEvaluateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="访问途径" align="center" prop="accessMethodName" v-if="columns[14].visible" />

      <el-table-column label="部署位置" align="center" prop="deployLocation" v-if="columns[19].visible" />
      <el-table-column label="机房地址" align="center" prop="machineRoomAddress" v-if="columns[20].visible" />
      <el-table-column label="机房主管单位名称" align="center" prop="machineRoomManager" v-if="columns[21].visible" />
      <el-table-column label="操作系统" align="center" prop="osTypeName" v-if="columns[22].visible" />
      <el-table-column label="操作系统版本" align="center" prop="osVersion" v-if="columns[23].visible" />
      <el-table-column label="中间件" align="center" prop="middlewareTypeName" v-if="columns[24].visible" />
      <el-table-column label="中间件版本" align="center" prop="middlewareVersion" v-if="columns[25].visible" />
      <el-table-column label="数据库" align="center" prop="databaseTypeName" v-if="columns[26].visible" />
      <el-table-column label="数据库版本" align="center" prop="databaseVersion" v-if="columns[27].visible" />
      <el-table-column label="应用软件产品" align="center" prop="appProductName" v-if="columns[28].visible" />
      <el-table-column label="应用产品版本" align="center" prop="appVersion" v-if="columns[29].visible" />
      <el-table-column label="联系人" align="center" prop="contactPerson" v-if="columns[30].visible" />
      <el-table-column label="办公电话" align="center" prop="officePhone" v-if="columns[31].visible" />
      <el-table-column label="手机号" align="center" prop="mobilePhone" v-if="columns[32].visible" />
      <el-table-column label="系统开发单位" align="center" prop="developCompany" v-if="columns[33].visible" />
      <el-table-column label="系统运维单位" align="center" prop="maintainCompany" v-if="columns[34].visible" />
      <el-table-column label="系统业务主管单位" align="center" prop="businessManager" v-if="columns[35].visible" />
      <el-table-column label="是否已开展商用密码应用安全性评估" align="center" prop="isSecurityEvaluatedName" v-if="columns[36].visible" />
      <el-table-column label="最近密评时间" align="center" prop="lastSecurityEvaluateTime" width="180" v-if="columns[37].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastSecurityEvaluateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="系统用户数量" align="center" prop="userCount" v-if="columns[38].visible" />
      <el-table-column label="云服务安全评估" align="center" prop="isCloudEvaluatedName" v-if="columns[39].visible" />
      <el-table-column label="状态" align="center" prop="status" v-if="columns[40].visible" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:asset:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:asset:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="组织架构" prop="orgId">
              <el-select
                v-model="form.orgId"
                placeholder="请选择组织架构"
                filterable
                style="width: 100%"
              >
                <el-option
                  v-for="item in deptTreeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.deptId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位类型" prop="orgType">
              <el-select v-model="form.orgType" placeholder="请选择单位类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dept_type"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="系统名称" prop="systemName">
              <el-input v-model="form.systemName" placeholder="请输入系统名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="系统类型" prop="systemType">
              <el-select v-model="form.systemType" placeholder="请选择系统类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_type"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="信息类别" prop="infoType">
              <el-select v-model="form.infoType" placeholder="请选择信息类别" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_info_type"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否定级" prop="isClassified">
              <el-select v-model="form.isClassified" placeholder="请选择是否定级" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_is_dj"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="系统级别" prop="systemLevel">
              <el-select v-model="form.systemLevel" placeholder="请选择系统级别" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_level"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="信息安全等级" prop="businessSecurityLevel">
              <el-select v-model="form.businessSecurityLevel" placeholder="请选择业务信息安全等级" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_sc_level"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="系统安全等级" prop="serviceSecurityLevel">
              <el-select v-model="form.serviceSecurityLevel" placeholder="请选择系统服务安全等级" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_fw_level"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="是否备案" prop="isFiled">
              <el-select v-model="form.isFiled" placeholder="请选择是否备案" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_is_ba"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备案编号" prop="filingNumber">
              <el-input v-model="form.filingNumber" placeholder="请输入备案编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否已测评" prop="isEvaluated">
              <el-select v-model="form.isEvaluated" placeholder="请选择是否已测评" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_is_pc"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="最近测评时间" prop="lastEvaluateTime">
              <el-date-picker
                v-model="form.lastEvaluateTime"
                type="date"
                placeholder="选择最近测评时间"
                style="width: 100%"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="访问途径" prop="accessMethod">
              <el-select v-model="form.accessMethod" placeholder="请选择访问途径" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_acc_type"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="部署位置" prop="deployLocation">
              <el-input v-model="form.deployLocation" placeholder="请输入部署位置" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="机房地址" prop="machineRoomAddress">
              <el-input v-model="form.machineRoomAddress" placeholder="请输入机房地址" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="机房主管单位" prop="machineRoomManager">
              <el-input v-model="form.machineRoomManager" placeholder="请输入机房主管单位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作系统" prop="osType">
              <el-select v-model="form.osType" placeholder="请选择操作系统" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_xt"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="操作系统版本" prop="osVersion">
              <el-input v-model="form.osVersion" placeholder="请输入操作系统版本" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="中间件" prop="middlewareType">
              <el-select v-model="form.middlewareType" placeholder="请选择中间件" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_zjj"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="中间件版本" prop="middlewareVersion">
              <el-input v-model="form.middlewareVersion" placeholder="请输入中间件版本" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="数据库" prop="databaseType">
              <el-select v-model="form.databaseType" placeholder="请选择数据库" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_db"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="数据库版本" prop="databaseVersion">
              <el-input v-model="form.databaseVersion" placeholder="请输入数据库版本" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应用软件产品" prop="appProduct">
              <el-select v-model="form.appProduct" placeholder="请选择应用软件产品" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_app"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="应用产品版本" prop="appVersion">
              <el-input v-model="form.appVersion" placeholder="请输入应用产品版本" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="办公电话" prop="officePhone">
              <el-input v-model="form.officePhone" placeholder="请输入办公电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="手机号" prop="mobilePhone">
              <el-input v-model="form.mobilePhone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="系统开发单位" prop="developCompany">
              <el-input v-model="form.developCompany" placeholder="请输入系统开发单位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="系统运维单位" prop="maintainCompany">
              <el-input v-model="form.maintainCompany" placeholder="请输入系统运维单位" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="业务主管单位" prop="businessManager">
              <el-input v-model="form.businessManager" placeholder="请输入系统业务主管单位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="密码应用评估" prop="isSecurityEvaluated">
              <el-select v-model="form.isSecurityEvaluated" placeholder="请选择是否已开展商用密码应用安全性评估" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_is_pwd"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最近密评时间" prop="lastSecurityEvaluateTime">
              <el-date-picker
                v-model="form.lastSecurityEvaluateTime"
                type="date"
                placeholder="选择最近密评时间"
                style="width: 100%"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="系统用户数量" prop="userCount">
              <el-input v-model="form.userCount" placeholder="请输入系统用户数量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="云服务安全评估" prop="isCloudEvaluated">
              <el-select v-model="form.isCloudEvaluated" placeholder="请选择云服务安全评估" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_is_yun"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24" style="text-align: center">
            <el-button type="primary" @click="showHostInfo = !showHostInfo">
              {{ showHostInfo ? '收起主机信息' : '展开主机信息' }}
            </el-button>
          </el-col>
        </el-row>

        <div v-if="showHostInfo">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-divider content-position="left">主机信息</el-divider>
            </el-col>
          </el-row>

          <div v-for="(host, index) in form.sysAssetGx" :key="index" class="host-item">
            <el-row :gutter="20">
              <el-col :span="22">
                <el-row :gutter="20">
                  <el-col :span="8">
                    <el-form-item label="访问域名" :prop="'sysAssetGx.' + index + '.accessDomain'" :rules="rules['sysAssetGx.0.accessDomain']">
                      <el-input v-model="host.accessDomain" placeholder="请输入访问域名" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="访问IP地址" :prop="'sysAssetGx.' + index + '.accessIp'" :rules="rules['sysAssetGx.0.accessIp']">
                      <el-input v-model="host.accessIp" placeholder="请输入访问IP地址" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="本地开放端口" :prop="'sysAssetGx.' + index + '.localPort'" :rules="rules['sysAssetGx.0.localPort']">
                      <el-input v-model="host.localPort" placeholder="请输入本地开放端口" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="8">
                    <el-form-item label="公网映射端口" :prop="'sysAssetGx.' + index + '.publicMappedPort'">
                      <el-input v-model="host.publicMappedPort" placeholder="请输入公网映射服务端口" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="服务器IP地址" :prop="'sysAssetGx.' + index + '.serverIp'" :rules="rules['sysAssetGx.0.serverIp']">
                      <el-input v-model="host.serverIp" placeholder="请输入服务器IP地址" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="是否有效" :prop="'sysAssetGx.' + index + '.isActive'">
                      <el-select v-model="host.isActive" placeholder="请选择是否有效" style="width: 100%">
                        <el-option label="是" value="1" />
                        <el-option label="否" value="0" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-col>
              <el-col :span="2" class="host-operation">
                <el-button type="danger" icon="el-icon-delete" circle @click="removeHostItem(index)" v-if="form.sysAssetGx.length > 1"></el-button>
              </el-col>
            </el-row>
            <el-divider v-if="index !== form.sysAssetGx.length - 1"></el-divider>
          </div>

          <el-row :gutter="20">
            <el-col :span="24" style="text-align: center">
              <el-button type="primary" icon="el-icon-plus" @click="addHostItem">添加主机信息</el-button>
            </el-col>
          </el-row>
        </div>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-divider></el-divider>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAsset, getAsset, delAsset, addAsset, updateAsset } from "@/api/admin/asset";
import { listDept } from "@/api/system/dept";
import { getDicts } from "@/api/system/dict/data";
import { listGx } from "@/api/system/gx";

export default {
  name: "Asset",
  dicts: ['dept_type', 'sys_type', 'sys_is_dj', 'sys_level', 'sys_sc_level', 'sys_fw_level', 'sys_is_ba', 'sys_is_pc',"sys_acc_type","sys_xt","sys_zjj","sys_db","sys_app","sys_is_pwd","sys_is_yun","sys_status", "sys_info_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 显示主机信息
      showHostInfo: false,
      // 总条数
      total: 0,
      // 资产信息表格数据
      assetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgId: null,
        orgType: null,
        systemName: null,
        systemLevel: null,
        systemType: null,
        infoType: null,
        isClassified: null,
        businessSecurityLevel: null,
        serviceSecurityLevel: null,
        isFiled: null,
        filingNumber: null,
        isEvaluated: null,
        lastEvaluateTime: null,
        accessMethod: null,
        deployLocation: null,
        machineRoomAddress: null,
        machineRoomManager: null,
        osType: null,
        osVersion: null,
        middlewareType: null,
        middlewareVersion: null,
        databaseType: null,
        databaseVersion: null,
        appProduct: null,
        appVersion: null,
        contactPerson: null,
        officePhone: null,
        mobilePhone: null,
        developCompany: null,
        maintainCompany: null,
        businessManager: null,
        isSecurityEvaluated: null,
        lastSecurityEvaluateTime: null,
        userCount: null,
        isCloudEvaluated: null,
        status: null,
        sysAssetGx: {
          accessDomain: null,
          accessIp: null,
          localPort: null,
          publicMappedPort: null,
          serverIp: null,
          isActive: null
        }
      },
      // 表单参数
      form: {
        assetId: null,
        orgId: null,
        orgType: null,
        systemName: null,
        systemType: null,
        infoType: null,
        isClassified: null,
        systemLevel: null,
        businessSecurityLevel: null,
        serviceSecurityLevel: null,
        isFiled: null,
        filingNumber: null,
        isEvaluated: null,
        lastEvaluateTime: null,
        accessMethod: null,
        deployLocation: null,
        machineRoomAddress: null,
        machineRoomManager: null,
        osType: null,
        osVersion: null,
        middlewareType: null,
        middlewareVersion: null,
        databaseType: null,
        databaseVersion: null,
        appProduct: null,
        appVersion: null,
        contactPerson: null,
        officePhone: null,
        mobilePhone: null,
        developCompany: null,
        maintainCompany: null,
        businessManager: null,
        isSecurityEvaluated: null,
        lastSecurityEvaluateTime: null,
        userCount: null,
        isCloudEvaluated: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        sysAssetGx: [{
          accessDomain: '',
          accessIp: '',
          localPort: '',
          publicMappedPort: '',
          serverIp: '',
          isActive: '1'
        }]
      },
      // 表单校验
      rules: {
        orgId: [
          { required: true, message: "组织架构不能为空", trigger: "change" }
        ],
        orgType: [
          { required: true, message: "单位类型不能为空", trigger: "change" }
        ],
        systemName: [
          { required: true, message: "系统名称不能为空", trigger: "blur" }
        ],
        systemType: [
          { required: true, message: "系统类型不能为空", trigger: "change" }
        ],
        isClassified: [
          { required: true, message: "是否定级不能为空", trigger: "blur" }
        ],
        isFiled: [
          { required: true, message: "是否备案不能为空", trigger: "blur" }
        ],
        isEvaluated: [
          { required: true, message: "是否已测评不能为空", trigger: "blur" }
        ],
        accessMethod: [
          { required: true, message: "访问途径不能为空", trigger: "blur" }
        ],
        deployLocation: [
          { required: true, message: "部署位置不能为空", trigger: "blur" }
        ],
        osType: [
          { required: true, message: "操作系统不能为空", trigger: "change" }
        ],
        osVersion: [
          { required: true, message: "操作系统版本不能为空", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        officePhone: [
          { required: true, message: "办公电话不能为空", trigger: "blur" }
        ],
        mobilePhone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        developCompany: [
          { required: true, message: "系统开发单位不能为空", trigger: "blur" }
        ],
        maintainCompany: [
          { required: true, message: "系统运维单位不能为空", trigger: "blur" }
        ],
        businessManager: [
          { required: true, message: "系统业务主管单位不能为空", trigger: "blur" }
        ],
        isSecurityEvaluated: [
          { required: true, message: "是否已开展商用密码应用安全性评估不能为空", trigger: "blur" }
        ],
        userCount: [
          { required: true, message: "系统用户数量不能为空", trigger: "blur" }
        ],
        isCloudEvaluated: [
          { required: true, message: "云服务安全评估不能为空", trigger: "blur" }
        ],
        'sysAssetGx.0.accessDomain': [
          { required: true, message: "访问域名不能为空", trigger: "blur" }
        ],
        'sysAssetGx.0.accessIp': [
          { required: true, message: "访问IP地址不能为空", trigger: "blur" }
        ],
        'sysAssetGx.0.localPort': [
          { required: true, message: "本地开放端口不能为空", trigger: "blur" }
        ],
        'sysAssetGx.0.serverIp': [
          { required: true, message: "服务器IP地址不能为空", trigger: "blur" }
        ]
      },
      // 部门列表
      deptOptions: [],
      // 处理后的部门列表（带有层级关系）
      deptTreeOptions: [],
      // 列信息
      columns: [
        { key: 0, label: `资产ID`, visible: true },
        { key: 1, label: `组织架构`, visible: true },
        { key: 2, label: `单位类型`, visible: true },
        { key: 3, label: `系统名称`, visible: true },
        { key: 4, label: `系统类型`, visible: true },
        { key: 5, label: `信息类别`, visible: true },
        { key: 6, label: `是否定级`, visible: true },
        { key: 7, label: `系统级别`, visible: true },
        { key: 8, label: `信息安全等级`, visible: true },
        { key: 9, label: `系统安全等级`, visible: true },
        { key: 10, label: `是否备案`, visible: true },
        { key: 11, label: `备案编号`, visible: true },
        { key: 12, label: `是否已测评`, visible: true },
        { key: 13, label: `最近测评时间`, visible: true },
        { key: 14, label: `访问途径`, visible: true },
        { key: 15, label: `访问域名`, visible: true },
        { key: 16, label: `访问IP地址`, visible: true },
        { key: 17, label: `本地开放端口`, visible: true },
        { key: 18, label: `公网映射服务端口`, visible: true },
        { key: 19, label: `服务器IP地址`, visible: true },
        { key: 20, label: `部署位置`, visible: true },
        { key: 21, label: `机房地址`, visible: true },
        { key: 22, label: `机房主管单位名称`, visible: true },
        { key: 23, label: `操作系统`, visible: true },
        { key: 24, label: `操作系统版本`, visible: true },
        { key: 25, label: `中间件`, visible: true },
        { key: 26, label: `中间件版本`, visible: true },
        { key: 27, label: `数据库`, visible: true },
        { key: 28, label: `数据库版本`, visible: true },
        { key: 29, label: `应用软件产品`, visible: true },
        { key: 30, label: `应用产品版本`, visible: true },
        { key: 31, label: `联系人`, visible: true },
        { key: 32, label: `办公电话`, visible: true },
        { key: 33, label: `手机号`, visible: true },
        { key: 34, label: `系统开发单位`, visible: true },
        { key: 35, label: `系统运维单位`, visible: true },
        { key: 36, label: `系统业务主管单位`, visible: true },
        { key: 37, label: `是否已开展商用密码应用安全性评估`, visible: true },
        { key: 38, label: `最近密评时间`, visible: true },
        { key: 39, label: `系统用户数量`, visible: true },
        { key: 40, label: `云服务安全评估`, visible: true },
        { key: 41, label: `状态`, visible: true }
      ],
      // 字典数据
      dict: {
        type: {
          dept_type: [],
          sys_type: [],
          sys_is_dj: [],
          sys_level: [],
          sys_sc_level: [],
          sys_fw_level: [],
          sys_is_ba: [],
          sys_is_pc: [],
          sys_acc_type: [],
          sys_xt: [],
          sys_zjj: [],
          sys_db: [],
          sys_app: [],
          sys_is_pwd: [],
          sys_is_yun: [],
          sys_status: [],
          sys_info_type: [],
          sys_org_type: []
        }
      },
      // 展开行键
      expandRowKeys: [],
    };
  },
  created() {
    this.getList();
    this.getDeptList();
    // 获取字典数据
    this.getDict();
  },
  methods: {
    /** 查询部门列表 */
    getDeptList() {
      listDept().then(response => {
        this.deptOptions = response.data;
        // 处理部门数据，生成带有层级关系的选项
        this.deptTreeOptions = this.handleDeptOptions(this.deptOptions);
      });
    },
    /** 处理部门数据，生成带有层级关系的选项 */
    handleDeptOptions(depts) {
      // 构建部门映射表
      const deptMap = {};
      depts.forEach(dept => {
        deptMap[dept.deptId] = dept;
      });

      // 生成层级选项
      const options = [];
      depts.forEach(dept => {
        let fullName = dept.deptName;
        let parentId = dept.parentId;

        // 向上查找父部门
        while (parentId !== 0 && deptMap[parentId]) {
          const parent = deptMap[parentId];
          fullName = parent.deptName + '/' + fullName;
          parentId = parent.parentId;
        }

        options.push({
          deptId: dept.deptId,
          label: fullName,
          value: dept.deptId
        });
      });

      // 按照部门的层级路径排序
      options.sort((a, b) => a.label.localeCompare(b.label));

      return options;
    },
    /** 查询资产信息列表 */
    getList() {
      this.loading = true;
      this.expandRowKeys = []; // 重置展开行
      listAsset(this.queryParams).then(response => {
        this.assetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取关联信息列表 */
    getGxList(row) {
      // 如果已经在加载中，不重复加载
      if (row.gxLoading) {
        return;
      }
      this.$set(row, 'gxLoading', true);
      listGx({ assetId: row.assetId }).then(response => {
        const gxList = Array.isArray(response) ? response : [response];
        this.$set(row, 'gxList', gxList);
        // 如果有数据，保持展开状态
        if (gxList && gxList.length > 0 && !this.expandRowKeys.includes(row.assetId)) {
          this.expandRowKeys.push(row.assetId);
        }
      }).catch(error => {
        console.error('获取关联信息失败:', error);
        this.$set(row, 'gxList', []);
        this.$modal.msgError('获取关联信息失败');
      }).finally(() => {
        this.$set(row, 'gxLoading', false);
      });
    },
    /** 处理展开行事件 */
    handleExpandChange(row, expandedRows) {
      const index = this.expandRowKeys.indexOf(row.assetId);
      if (expandedRows.includes(row)) {
        // 展开操作
        if (index === -1) {
          this.expandRowKeys.push(row.assetId);
        }
        if (!row.gxList) {
          this.getGxList(row);
        }
      } else {
        // 收起操作
        if (index !== -1) {
          this.expandRowKeys.splice(index, 1);
        }
      }
    },
    /** 查询字典数据 */
    getDict() {
      getDicts("dept_type").then(response => {
        this.dict.type.dept_type = response.data;
      });
      getDicts("sys_type").then(response => {
        this.dict.type.sys_type = response.data;
      });
      getDicts("sys_is_dj").then(response => {
        this.dict.type.sys_is_dj = response.data;
      });
      getDicts("sys_level").then(response => {
        this.dict.type.sys_level = response.data;
      });
      getDicts("sys_sc_level").then(response => {
        this.dict.type.sys_sc_level = response.data;
      });
      getDicts("sys_fw_level").then(response => {
        this.dict.type.sys_fw_level = response.data;
      });
      getDicts("sys_is_ba").then(response => {
        this.dict.type.sys_is_ba = response.data;
      });
      getDicts("sys_is_pc").then(response => {
        this.dict.type.sys_is_pc = response.data;
      });

      getDicts("sys_acc_type").then(response => {
        this.dict.type.sys_acc_type = response.data;
      });
      getDicts("sys_xt").then(response => {
        this.dict.type.sys_xt = response.data;
      });
      getDicts("sys_zjj").then(response => {
        this.dict.type.sys_zjj = response.data;
      });
      getDicts("sys_db").then(response => {
        this.dict.type.sys_db = response.data;
      });
      getDicts("sys_app").then(response => {
        this.dict.type.sys_app = response.data;
      });
      getDicts("sys_is_pwd").then(response => {
        this.dict.type.sys_is_pwd = response.data;
      });
      getDicts("sys_is_yun").then(response => {
        this.dict.type.sys_is_yun = response.data;
      });
      getDicts("sys_status").then(response => {
        this.dict.type.sys_status = response.data;
      });
      getDicts("sys_info_type").then(response => {
        this.dict.type.sys_info_type = response.data;
      });
      getDicts("sys_org_type").then(response => {
        this.dict.type.sys_org_type = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.showHostInfo = false;
      this.form = {
        assetId: null,
        orgId: null,
        orgType: null,
        systemName: null,
        systemType: null,
        infoType: null,
        isClassified: null,
        systemLevel: null,
        businessSecurityLevel: null,
        serviceSecurityLevel: null,
        isFiled: null,
        filingNumber: null,
        isEvaluated: null,
        lastEvaluateTime: null,
        accessMethod: null,
        deployLocation: null,
        machineRoomAddress: null,
        machineRoomManager: null,
        osType: null,
        osVersion: null,
        middlewareType: null,
        middlewareVersion: null,
        databaseType: null,
        databaseVersion: null,
        appProduct: null,
        appVersion: null,
        contactPerson: null,
        officePhone: null,
        mobilePhone: null,
        developCompany: null,
        maintainCompany: null,
        businessManager: null,
        isSecurityEvaluated: null,
        lastSecurityEvaluateTime: null,
        userCount: null,
        isCloudEvaluated: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        sysAssetGx: [{
          accessDomain: '',
          accessIp: '',
          localPort: '',
          publicMappedPort: '',
          serverIp: '',
          isActive: '1'
        }]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assetId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.showHostInfo = false;
      this.open = true;
      this.title = "添加资产信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const assetId = row.assetId || this.ids;
      // 先打开表单
      this.open = true;
      this.title = "修改资产信息";
      
      // 获取资产基本信息
      getAsset(assetId).then(response => {
        this.form = response.data;
        
        // 确保组织架构数据已加载
        if (this.deptTreeOptions.length === 0) {
          this.getDeptList();
        }
        
        // 处理关联数据
        if (this.form.sysAssetGx && this.form.sysAssetGx.length > 0) {
          // 将数字类型的isActive转为字符串类型
          this.form.sysAssetGx = this.form.sysAssetGx.map(item => ({
            ...item,
            isActive: item.isActive.toString()
          }));
          // 如果有主机信息数据，自动展开主机信息区域
          this.$nextTick(() => {
            this.showHostInfo = true;
          });
        } else {
          // 如果没有关联数据，初始化一个空的关联信息，并隐藏主机信息区域
          this.form.sysAssetGx = [{
            accessDomain: '',
            accessIp: '',
            localPort: '',
            publicMappedPort: '',
            serverIp: '',
            isActive: '1'
          }];
          this.showHostInfo = false;
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assetId != null) {
            updateAsset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAsset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assetIds = row.assetId || this.ids;
      this.$modal.confirm('是否确认删除资产信息编号为"' + assetIds + '"的数据项？').then(function() {
        return delAsset(assetIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('admin/asset/export', {
        ...this.queryParams
      }, `asset_${new Date().getTime()}.xlsx`)
    },
    /** 新增主机信息项 */
    addHostItem() {
      this.form.sysAssetGx.push({
        accessDomain: '',
        accessIp: '',
        localPort: '',
        publicMappedPort: '',
        serverIp: '',
        isActive: '1'
      });
    },
    /** 删除主机信息项 */
    removeHostItem(index) {
      this.form.sysAssetGx.splice(index, 1);
    },
  }
};
</script>

<style scoped>
.host-item {
  margin-bottom: 10px;
}
.host-operation {
  display: flex;
  align-items: center;
  height: 100%;
  padding-top: 30px;
}
</style>
